package 剑指offer;

/**
 * @author zhangjunfei
 * @date 2023/7/19 10:01
 */
public class O_67 {

    public static int strToInt(String str) {
        str = str.trim();
        int len = str.length();
        if (len == 0) {
            return 0;
        }
        int index = 0;
        int sign = 1;// 符号位
        if (str.charAt(index) == '+' || str.charAt(index) == '-') {
            sign = str.charAt(index) == '+' ? 1 : -1;
            index++;
        }
        int res = 0;
        for (; index < len; index++) {
            int digst = str.charAt(index) - '0';
            if (digst < 0 || digst > 9) {
                break;
            }
            if (res > Integer.MAX_VALUE / 10 || res == Integer.MAX_VALUE/10 && digst > Integer.MAX_VALUE % 10) {
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
            res = res * 10 + digst;

        }
        return sign * res;

    }


    public static void main(String[] args) {
        O_67.strToInt("42");
    }
}
